export function formatUTCDate(date: Date, format: string): string {
  const o = new Map<string, string>([
    ['M+', (date.getUTCMonth() + 1).toString()],
    ['d+', date.getUTCDate().toString()],
    ['h+', date.getUTCHours().toString()],
    ['m+', date.getUTCMinutes().toString()],
    ['s+', date.getUTCSeconds().toString()],
    ['q+', Math.floor((date.getUTCMonth() + 3) / 3).toString()],
    ['S', date.getUTCMilliseconds().toString()],
  ])
  if (/(y+)/.test(format)) {
    format = format.replace(
      RegExp.$1,
      (date.getUTCFullYear() + '').substr(4 - RegExp.$1.length)
    )
  }
  for (let k of o.keys()) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(
        RegExp.$1,
        RegExp.$1.length === 1
          ? o.get(k)!
          : ('00' + o.get(k)).substr(('' + o.get(k)).length)
      )
    }
  }
  return format
}

export function getQueryObject(params: string): Map<string, string> {
  let ret = new Map<string, string>()
  if (params.indexOf('?') !== -1) {
    params = params.substr(1)
  }
  let strs = params.split('&')
  for (var i = 0; i < strs.length; i++) {
    ret.set(strs[i].split('=')[0], decodeURIComponent(strs[i].split('=')[1]))
  }
  return ret
}

export function getSortedParams(map: Map<string, string>): string {
  return [...map]
    .sort()
    .map((i) => `${i[0]}=${encodeURIComponent(i[1])}`)
    .join('&')
}
